package com.gouaixin.bee.beeframework2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.activity.B6_ziliaoActivity;
import com.gouaixin.bee.beeframework2.adapter.guanzhubieAdapter;
import com.gouaixin.bee.beeframework2.bean.GUANZHU;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.gouaixin.bee.beeframework2.bean.BaseModel;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;


/**
 * Created by Administrator on 2015/3/9.
 */
//我关注的
public class B2_guanzhuBieFragment extends Fragment {
    View mainView;

    ListView listView;
    guanzhubieAdapter adapter;
    ArrayList<GUANZHU> data=new  ArrayList<GUANZHU>();



    /**
     *  在这里实现Fragment数据的缓加载.    
     */
    public boolean isVisible;

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {

        setData();

        setListener();

    }


    protected void onInvisible() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.b2_guanzhubie, null);
        adapter=new guanzhubieAdapter(getActivity());

        initView();




        return mainView;
    }

    private void setData() {
        String cid = PreferencesUtils.getString(getActivity(), "cid", "");
        BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
            @Override
            public String onCallBack(String url, String result) {
                if(url.equals(MyAPI.getguanzhulist)){
                    if(!result.equals("")){
                        data=new Gson().fromJson(result, new TypeToken<ArrayList<GUANZHU>>(){}.getType());
                        mLog.e("解析返回值：" + data.size());
                        adapter.setData(data);
                        listView.setAdapter(adapter);
                    }

                }

                return url;
            }
        });
        RequestParams params = new RequestParams();
        params.addBodyParameter("cid", cid);
        BaseModel.post(MyAPI.getguanzhulist, params);


    }

    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(!data.equals("")){
                    Intent it = new Intent().setClass(getActivity(), B6_ziliaoActivity.class);
                    it.putExtra("HerId",data.get(position).getId());
                    startActivity(it);
                }



            }
        });

    }

    private void initView() {
        listView = (ListView) mainView.findViewById(R.id.list);

    }
}
